package com.example.ssmlab1.controller;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Identity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.IdentityHashMap;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    //  private BasicDataSource basicDataSource;

//    public UserController(BasicDataSource basicDataSource) {
//        this.basicDataSource = basicDataSource;
//    }
//    @Autowired
//    //自动装配 ||
//    public void setBasicDataSource(BasicDataSource basicDataSource) {
//        this.basicDataSource = basicDataSource;
//    }

    @RequestMapping(value = "signIn", method = RequestMethod.POST)
    private String signIn(@RequestParam Integer id, @RequestParam String password) throws SQLException {
        //                 参数级别的注解
        Connection connection = DB.getConnection();
        String sql = "select * from db_mvc.employee where username=? and password=?";
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = DB.getConnection().prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                String username = resultSet.getString("username");
                User user = new User(id, username, password);

                request.getSession().setAttribute("id", id);
                request.getSession().setAttribute("username", username);
                request.getSession().setAttribute("password", password);
                return "redirect:/home.jsp";
            } else {
                request.setAttribute("error", "用户名或密码错误");
                return "/index.jsp";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DB.close(resultSet, preparedStatement, connection);
        }

    }

    @RequestMapping(value = "signUp", method = RequestMethod.POST)
    private String signUp(@RequestParam String username, @RequestParam String password) throws SQLException {
////        通过ioc 连接数据库
////        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext");
////        BasicDataSource basicDataSource = (BasicDataSource) applicationContext.getBean("basicDataSource");
        Connection connection = DB.getConnection();
        String sqlInsert = "insert into db_mvc.employee(username,password)values(?,?)";
        String sqlSelect = "select last_insert_id()";


        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = DB.getConnection().prepareStatement(sqlInsert);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.executeUpdate();

            preparedStatement = DB.getConnection().prepareStatement(sqlSelect);
            resultSet = preparedStatement.executeQuery();
            resultSet.next();
            int id = resultSet.getInt(1);
            request.setAttribute("message", "你的id是：" + id);
            return "/index.jsp";


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DB.close(resultSet, preparedStatement, connection);
        }


    }


}
